<?php
    require_once("connect.php");
		
	function createReferenceForNode(){
		global $dbcon;
		$node_id_ = (integer)$_POST["node_id"];
		$reference_ = pg_escape_string(utf8_encode($_POST["reference"]));
		
		$query_ = "INSERT INTO references_for_nodes (node_id, reference) VALUES ($node_id_, '$reference_')";
		
		pg_query($dbcon, $query_);
	}
	
	function editReferenceForNode(){
		global $dbcon;
		$id_ = (integer)$_POST["id"];
		$node_id_ = (integer)$_POST["node_id"];
		$reference_ = pg_escape_string(utf8_encode($_POST["reference"]));
		
		$query_ = "UPDATE references_for_nodes SET node_id=$node_id_, reference='$reference_' WHERE id=$id_";
		
		pg_query($dbcon, $query_);
	}
	
	function deleteReference(){
		global $dbcon;
		$id_ = (integer)$_GET["id"];
		pg_query($dbcon, "DELETE FROM references_for_nodes WHERE id=$id_");
	}
	
	function printReferencesAsList($idparam){
		global $dbcon;
		$id_ = (integer)$idparam;
		$references_ = pg_query($dbcon, "SELECT * FROM references_for_nodes WHERE node_id=$id_");
		echo "<ul>";
		while($ref_ = pg_fetch_assoc($references_)){
			echo "<li>";
			echo utf8_decode($ref_["reference"]);
			echo " (<a href=\"edit_reference.php?id=".$ref_["id"]."\">Edit</a> / <a href=\"confirm_deletion_reference.php?id=".$ref_["id"]."&node_id=".$ref_["node_id"]."\">Delete</a>)";
			echo "</li>";
		}
		echo "</ul>";
	}
	
	function getReference($idparam){
		global $dbcon;
		$id_ = (integer)$idparam;
		return pg_fetch_assoc(pg_query($dbcon, "SELECT * FROM references_for_nodes WHERE id=$id_"));
	}
?>